package com.ithema.reggie.controller;

import com.ithema.reggie.common.R;
import com.ithema.reggie.dto.DishDto;
import com.ithema.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    DishService dishService;

    /**
     * 保存菜品
     * @param session 会话域对象
     * @param dishDto  dto对象
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        //1.获取登录人id
        Long userId = (Long) session.getAttribute("employee");

        //2.给dishDto封装创建人和修改人id
        dishDto.setCreateUser(userId);
        dishDto.setUpdateUser(userId);

        //3.调用service完成保存
        return dishService.save(dishDto);

    }

    /**
     * 菜品信息分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("page")
    public R findByPage(@RequestParam("page")Integer pageNum,Integer pageSize,String name){

        return dishService.findByPage(pageNum,pageSize,name);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }

    /**
     *作用：修改菜品
     *
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto,HttpSession session){
        Long userId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(userId);

        return dishService.update(dishDto);
    }

    /**
     * 方法作用： 根据菜品类别的id查找的菜品
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    public R findByCategoryId(Long categoryId){
        return dishService.findByCategoryId(categoryId);
    }
}
